<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="css/site.css">
</head>
<body>
		
	<jsp:include page="header.jsp"></jsp:include>
	
	<jsp:include page="menu.jsp"></jsp:include>
	<div class="container">
		<h1>员工列表</h1>
		
		<form id="search" >
			姓名:<input type="text" name="ename">
			电话:<input type="text" name="ephone">
			部门:<select name="did">
					<option value="">请选择部门</option>
					
				</select>
			<input type="submit" value="搜索">
		</form>
		<br>
		<table border="1px" width="100%">
			<thead>
				<tr>
					<th>ID</th>
					<th>姓名</th>
					<th>照骗</th>
					<th>电话</th>
					<th>生日</th>
					<th>部门</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>
		
	</div>
	
<!-- ajax 请求所有的员工信息 -->
<script type="text/javascript" src="js/jquery-2.1.4.js"></script>
<script type="text/javascript">

	// 表单绑定提交事件
	$("#search").submit(function(){
		
		var ename = $("input[name='ename']").val();
		var ephone = $("input[name='ephone']").val();
		var did = $("select[name='did']").val();
		
		// 异步提交
		// 获取所有员工信息
		searchEmp(ename, ephone, did);
		
		return false;
	});
	
	searchEmp();
	
	
	// 获取部门列表
	$.ajax({
		url : 'dept.do?action=getAllJson',
		data : {},
		type : 'post',
		dataType : 'json',
		success : function(data){
			var html = "<option value=''>请选择部门</option>";
			$.each(data, function(index, item){
				html += "<option value='"+item.did+"'>"+item.dname+"</option>";
			})
			
			$("select[name='did']").html(html);
		},
		error : function(){
			
		}
	});
	
	
	// 获取所有员工信息
	function searchEmp(ename, ephone, did){
		$.ajax({
			url: 'emp.do?action=searchJson',
			data : {ename: ename, ephone: ephone, did: did},
			type : 'POST',
			dataType : 'json',
			success : function(data){
				// 遍历data
				var html = "";
				$.each(data, function(index, item){
					html += "<tr>"+
						"<td>"+item.eid+"</td>"+
						"<td>"+item.ename+"</td>"+
						"<td>"+((item.ephoto!=null)?"<img width='50px' src='upload/"+item.ephoto+"'>":"")+"</td>"+
						"<td>"+item.ephone+"</td>"+
						"<td>"+item.ebirthday+"</td>"+
						"<td>"+item.dname+"</td>"+
						"<td><a href='emp.do?action=modify&eid="+item.eid+"'>修改</a>"+
								"<a href='emp.do?action=remove&eid="+item.eid+"'>删除</a></td>"+
						"</tr>";
				});
				
				$("tbody").html(html);
				
			},
			error : function(){
				
			}
		});
	}
	
</script>	
</body>
</html>